package com.huaxin.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huaxin.device.constant.AlarmMessageParams;
import com.huaxin.device.dto.ResponseResult;
import com.huaxin.device.dto.request.AlarmThresholdTemplateCreateReqDTO;
import com.huaxin.device.dto.request.AlarmThresholdTemplatePageListReqDTO;
import com.huaxin.device.dto.request.AlarmThresholdTemplateUpdateReqDTO;
import com.huaxin.device.models.AlarmMessageParam;
import com.huaxin.device.models.AlarmThresholdTemplate;
import com.huaxin.device.service.AlarmThresholdTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.springframework.util.StringUtils.hasText;

@Tag(name = "报警阈值模板")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/device/alarmThresholdTemplate")
public class AlarmThresholdTemplateController {

    private final AlarmThresholdTemplateService service;

    @Operation(summary = "创建报警阈值模板", description = "创建新的报警阈值模板")
    @PostMapping("/create")
    public ResponseResult<AlarmThresholdTemplate> create(@RequestBody @Valid AlarmThresholdTemplateCreateReqDTO in) {
        return service.create(in);
    }

    @Operation(summary = "获取报警阈值模板", description = "根据ID获取报警阈值模板")
    @GetMapping("/get")
    public ResponseResult<AlarmThresholdTemplate> get(
            @Parameter(description = "模板ID", example = "tmpl_001")
            @RequestParam(value = "id", required = true) String id) {
        if (!hasText(id)) {
            return ResponseResult.parameterError(null, "ID不能为空");
        }
        AlarmThresholdTemplate entity = service.getByTemplateId(id);
        if (entity == null) {
            return ResponseResult.notFound(null, "未找到指定的报警阈值模板");
        }
        return ResponseResult.ok(entity);
    }

    @Operation(summary = "更新报警阈值模板", description = "更新报警阈值模板信息")
    @PostMapping("/update")
    public ResponseResult<String> update(@RequestBody @Valid AlarmThresholdTemplateUpdateReqDTO in) {
        return service.update(in);
    }

    @Operation(summary = "删除报警阈值模板", description = "删除指定ID的报警阈值模板（系统默认模板不可删除）")
    @DeleteMapping("/{id}/delete")
    public ResponseResult<String> delete(
            @Parameter(description = "模板ID", example = "tmpl_001")
            @PathVariable String id) {
        return service.delete(id);
    }

    @Operation(summary = "分页查询报警阈值模板", description = "支持按设备类型、是否启用、关键字等条件分页查询")
    @PostMapping("/page")
    public ResponseResult<IPage<AlarmThresholdTemplate>> pageQuery(@RequestBody @Valid AlarmThresholdTemplatePageListReqDTO in) {
        return service.pageQuery(in);
    }

    @Operation(summary = "获取报警消息模板可用参数")
    @GetMapping("/messageParams")
    public ResponseResult<List<AlarmMessageParam>> getMessageParams() {
        return ResponseResult.ok(AlarmMessageParams.getAll());
    }
}